/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.awt.geom.Point2D;
import java.util.StringTokenizer;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.geo.GLine;
import br.ufmg.dees.insane.model.geo.GPoint;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A command to add lines, or just the initial or final point of a line,
 * using the keyboard to enter data (the coords of the points).
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since January 2004
 */
public class AddLineKeybCommand implements Command, Cloneable {
    
    private InsaneInternalInterface parent;
    private Controller controller;
    private DrawingArea da;
	private StringTokenizer stk;
	private AddLineMouseCommand cmd;
    
//*****************************************************************************
	
    /** Constructor method
	* @param parent The parent of this command.
	*/
    public AddLineKeybCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
        controller = this.parent.getController();
        da = this.parent.getDrawing();
		cmd = (AddLineMouseCommand) this.parent.getDrawing().getClickedCommand();
    }
    
//*****************************************************************************
	
	/** The execute method.<br>
	* It adds lines, or just the initial or final point of a line,
	* using the keyboard to enter data (the coords of the points).
	*/
    public void execute() {
        String str = parent.getConsole().getLastCommand();
		
		double x0 = 0;
		double y0 = 0;
		
		if (str.startsWith("@")) {
			String str2 = str.substring(1);
			stk = new StringTokenizer(str2, ", ");
			Point2D ip = parent.getConsole().getLastPoint();
			x0 = ip.getX();
			y0 = ip.getY();
		} else {
			stk = new StringTokenizer(str, ", ");
		}
		
		try {
			Point2D po = new Point2D.Double(x0+Double.parseDouble(stk.nextToken()), y0+Double.parseDouble(stk.nextToken()));
			cmd.setClicks(cmd.getClicks() + 1);
			
			if (cmd.getClicks() % 2 != 0){
				if (!controller.contains(po)){
					GPoint p = new GPoint(po);
					controller.add(p);
					da.repaint();
					cmd.addNewPoint((GPoint)controller.get(po));
					cmd.setInitialPoint((GPoint)controller.get(po));
					parent.addOutputText(parent.getResourceBundle().getString("cmIniPointCreated") + " " + cmd.getInitialPoint().getX() + ", " + cmd.getInitialPoint().getY());
				}else{
					cmd.setInitialPoint((GPoint)controller.get(po));
					parent.addOutputText(parent.getResourceBundle().getString("cmIniPointChosen") + " " + cmd.getInitialPoint().getX() + ", " + cmd.getInitialPoint().getY());
				}
				PreviewLineCommand pl = new PreviewLineCommand(parent);
				pl.setInitialPoint(da.getViewPoint(cmd.getInitialPoint()));
				da.setMovedCommand(pl);
				da.setRepainting(true);
				da.repaint();
				parent.addOutputText(parent.getResourceBundle().getString("cmSpecFinalPoint"));
				parent.getConsole().setLastPoint(new Point2D.Double(po.getX(), po.getY()));
			}else{
				if(po.getX()!=cmd.getInitialPoint().getX() || po.getY()!=cmd.getInitialPoint().getY()) {
					if (!controller.contains(po)){
						GPoint p = new GPoint(po);
						controller.add(p);
						da.repaint();
						cmd.addNewPoint((GPoint)controller.get(po));
						cmd.setFinalPoint((GPoint)controller.get(po));
						parent.addOutputText(parent.getResourceBundle().getString("cmFinalPointCreated") + " " + cmd.getFinalPoint().getX() + ", " + cmd.getFinalPoint().getY());
					}else{
						cmd.setFinalPoint((GPoint)controller.get(po));
						parent.addOutputText(parent.getResourceBundle().getString("cmFinalPointChosen") + " " + cmd.getFinalPoint().getX() + ", " + cmd.getFinalPoint().getY());
					}
					da.setMovedCommand(null);
					da.setRepainting(false);
					da.setShape(null);
					
					cmd.setLine(new GLine(cmd.getInitialPoint(), cmd.getFinalPoint()));
					controller.add(cmd.getLine());
					da.repaint();
					parent.addOutputText(parent.getResourceBundle().getString("cmSpecifyIniPoint"));
					parent.addToUndo((Command)cmd.clone());
					cmd.clearNewPoints();
					parent.getConsole().setLastPoint(new Point2D.Double(po.getX(), po.getY()));
				} else {
					parent.addOutputText(parent.getResourceBundle().getString("cmMustBeDifferent"));
					parent.addOutputText(parent.getResourceBundle().getString("cmSpecFinalPoint"));
					cmd.setClicks(cmd.getClicks() - 1);;
				}
			}
		} catch (Exception e) {
				parent.addOutputText(parent.getResourceBundle().getString("cmInvalidData"));
		}
    }
	
//*****************************************************************************
	
	/** Impossible to undo - Not implemented. */
    public void undo() {}
    
//*****************************************************************************
	
	/** Impossible to redo - Not implemented. */
    public void redo() {}
    
//*****************************************************************************
}

